/*
 * Copyright (C) 2012 Santiago Valdarrama
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.yuanmei.stockeyes.artifacts;

import static com.yuanmei.stockeyes.artifacts.Extensions.toValueOrEmptyString;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yuanmei.stockeyes.R;
import com.yuanmei.stockeyes.model.Stock;

public class StockOverviewManager {

	private View dividerImageView;
	private TextView priceTextView;
	private TextView SymbolTextView;
	private TextView codeTextView;
	private TextView changeTextView;

	private TextView openTextView;
	private TextView ask1TextView;
	private TextView bid1TextView;
	private TextView askVol1TextView;
	private TextView bidVol1TextView;

	private TextView highTextView;
	private TextView ask2TextView;
	private TextView bid2TextView;
	private TextView askVol2TextView;
	private TextView bidVol2TextView;

	private TextView lowTextView;	
	private TextView ask3TextView;
	private TextView bid3TextView;
	private TextView askVol3TextView;
	private TextView bidVol3TextView;

	//private TextView volumeLabelTextView;
	private TextView volumeTextView;
	private TextView ask4TextView;
	private TextView bid4TextView;
	private TextView askVol4TextView;
	private TextView bidVol4TextView;

	private TextView turnoverTextView;
	private TextView ask5TextView;
	private TextView bid5TextView;
	private TextView askVol5TextView;
	private TextView bidVol5TextView;

	//private TextView yestCloseTextView;

	public StockOverviewManager(ViewGroup parent) {
		this.dividerImageView = parent.findViewById(R.id.dividerImageView);
		this.priceTextView = (TextView) parent.findViewById(R.id.priceTextView);
		this.SymbolTextView = (TextView) parent.findViewById(R.id.SymbolTextView);
		this.codeTextView = (TextView) parent.findViewById(R.id.codeTextView);

		this.changeTextView = (TextView) parent.findViewById(R.id.changeTextView);
		this.openTextView = (TextView) parent.findViewById(R.id.openTextView);
		this.ask1TextView = (TextView) parent.findViewById(R.id.ask1TextView);
		this.bid1TextView = (TextView) parent.findViewById(R.id.bid1TextView);
		this.askVol1TextView = (TextView) parent.findViewById(R.id.askVol1TextView);
		this.bidVol1TextView = (TextView) parent.findViewById(R.id.bidVol1TextView);

		this.highTextView = (TextView) parent.findViewById(R.id.highTextView);
		this.ask2TextView = (TextView) parent.findViewById(R.id.ask2TextView);
		this.bid2TextView = (TextView) parent.findViewById(R.id.bid2TextView);
		this.askVol2TextView = (TextView) parent.findViewById(R.id.askVol2TextView);
		this.bidVol2TextView = (TextView) parent.findViewById(R.id.bidVol2TextView);


		this.lowTextView = (TextView) parent.findViewById(R.id.lowTextView);
		this.ask3TextView = (TextView) parent.findViewById(R.id.ask3TextView);
		this.bid3TextView = (TextView) parent.findViewById(R.id.bid3TextView);
		this.askVol3TextView = (TextView) parent.findViewById(R.id.askVol3TextView);
		this.bidVol3TextView = (TextView) parent.findViewById(R.id.bidVol3TextView);

		//this.volumeLabelTextView = (TextView) parent.findViewById(R.id.volumeLabelTextView);
		this.volumeTextView = (TextView) parent.findViewById(R.id.volumeTextView);
		this.ask4TextView = (TextView) parent.findViewById(R.id.ask4TextView);
		this.bid4TextView = (TextView) parent.findViewById(R.id.bid4TextView);
		this.askVol4TextView = (TextView) parent.findViewById(R.id.askVol4TextView);
		this.bidVol4TextView = (TextView) parent.findViewById(R.id.bidVol4TextView);

		this.turnoverTextView = (TextView) parent.findViewById(R.id.turnoverTextView);
		this.ask5TextView = (TextView) parent.findViewById(R.id.ask5TextView);
		this.bid5TextView = (TextView) parent.findViewById(R.id.bid5TextView);
		this.askVol5TextView = (TextView) parent.findViewById(R.id.askVol5TextView);
		this.bidVol5TextView = (TextView) parent.findViewById(R.id.bidVol5TextView);

		//this.yestCloseTextView = (TextView) parent.findViewById(R.id.yestCloseTextView);

	}

	public void setStock(Stock stock) {
		if (this.dividerImageView != null) {
			this.dividerImageView.setBackgroundResource(Extensions.dividerLineResourceId(stock));

			/*
			this.SymbolTextView.setText(stock.isMarketIndex()
				? stock.getName()
				: stock.getCode());
			*/
                     String stk_code;
			if (stock.isMarketIndex())
			{
				stk_code = stock.getCode().substring(3);
			}
			else
			{
				stk_code = stock.getCode().substring(2);
			}
			this.SymbolTextView.setText(stock.getName().toUpperCase());
			this.codeTextView.setText(stk_code);

  			this.priceTextView.setText(toValueOrEmptyString(stock.getStringPrice()));
			this.changeTextView.setText(toValueOrEmptyString(stock.getStringChangePercentage()));

			this.openTextView.setText(stock.getStringOpen());
			this.ask1TextView.setText(stock.getStringAsk1());
			this.askVol1TextView.setText(stock.getStringAskVol1());
			this.bid1TextView.setText(stock.getStringBid1());
			this.bidVol1TextView.setText(stock.getStringBidVol1());

			this.highTextView.setText(stock.getStringHigh());
			this.ask2TextView.setText(stock.getStringAsk2());
			this.bid2TextView.setText(stock.getStringBid2());
			this.askVol2TextView.setText(stock.getStringAskVol2());
			this.bidVol2TextView.setText(stock.getStringBidVol2());

			this.lowTextView.setText(stock.getStringLow());
			this.ask3TextView.setText(stock.getStringAsk3());
			this.bid3TextView.setText(stock.getStringBid3());
			this.askVol3TextView.setText(stock.getStringAskVol3());
			this.bidVol3TextView.setText(stock.getStringBidVol3());

			//this.volumeLabelTextView.setText(R.string.stock_overview_volume_label);
			this.volumeTextView.setText(stock.getStringVolume());
			this.ask4TextView.setText(stock.getStringAsk4());
			this.bid4TextView.setText(stock.getStringBid4());
			this.askVol4TextView.setText(stock.getStringAskVol4());
			this.bidVol4TextView.setText(stock.getStringBidVol4());

			this.turnoverTextView.setText(stock.getStringTurnover());
			this.ask5TextView.setText(stock.getStringAsk5());
			this.bid5TextView.setText(stock.getStringBid5());
			this.askVol5TextView.setText(stock.getStringAskVol5());
			this.bidVol5TextView.setText(stock.getStringBidVol5());

			//this.yestCloseTextView.setText(stock.getStringYestClose());
		}
	}

	public void setPrice(String value) {
		this.priceTextView.setText(value);
	}

}
